/**
* @version v 0.1.1 2012/03/18
* @package Nerv Widgets, NW3.js
* @subpackage Nerv Toolbox, toolbox.js
* @author Manuele Castellan <manuele.castellan@gmail.com>
*
* @copyright    Copyright (C) Copyright (c) 2009 Manuele Castellan. All rights reserved.
* @license        GNU/GPLv3, see LICENSE
*
*  This program is free software: you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation, either version 3 of the License, or
*  (at your option) any later version.
*
*  This program is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*
*/


NW3.toolbox = {
	Toolbox : function(params){
		var oWin = new NW3.Window;
		
		var obj = {
			title : params.title,
			object : oWin,
			position : ((params.align != undefined) ? params.align : 'left'),
			opened : false,
			
			width : params.width,
			height : params.height,
			
			contents : new Array(),
			buttons : new Array()
		}
		
		// differenza
		if(NW3.client.isIE()) h_diff = 19
		else h_diff = 35;
		
		obj.otabs = new NW3.tabs.Tabs({
			height: params.height - h_diff
		})
		
		obj.object.setClass('NWToolboxWindowMaker');
		obj.object.setWidth(obj.width);
		obj.object.setHeight(obj.height);
		
		obj.object.setDraggable(false);
		
		obj.alignRight = function(){
			obj.position = 'right';
		}
		
		obj.floatLeft = function(){
			distance = '-' + parseInt(obj.object.getHeight());
			obj.object.setTop(parseInt(distance));
			
			//obj.object.setTop('0');
			
			obj.object.setLeft('0');
		}
		
		obj.floatRight = function(){
			distance = '-' + parseInt(obj.object.getHeight());
			obj.object.setTop(parseInt(distance));
			
			distance = NW3.client.getWidth() - obj.object.getWidth() - 17;
			obj.object.setLeft(distance);
		}
		
		obj.quickShow = function(){
			obj.opened = true;
			
			distance = parseInt(obj.object.getHeight());
			
			obj.object.move(distance, 'V', 0);
			
			//obj.table.display(true);
		}
		
		obj.show = function(){
			obj.opened = true;
			
			distance = parseInt(obj.object.getHeight());
			
			obj.object.move(distance, 'V', 35);
			
			//obj.table.display(true);
		}
		
		obj.hide = function(){
			obj.opened = false;
			
			//obj.table.display(false);
			
			/*if(obj.position == 'right'){
				distance = parseInt(obj.object.getWidth());
				distance = parseInt(distance);
			}
			else {*/
				distance = parseInt('-' + obj.object.getWidth());
			//}
			obj.object.move(distance, 'V');
		}
		
		// aggiunge evento al bottone di chiusura
		obj.setButtonCloseEvent = function(f){
			obj.buttoncloseevent = f;
		}
		
		
		// aggiunge evento ai bottoni tabs
		obj.setButtonTabEvent = function(f){
			obj.buttontabevent = f;
		}
		
		// add contents
		obj.add = function(oNode){
			obj.contents.push(oNode);
		}
		
		obj.addHtml = function(str){
			obj.contents.push(new Array(str));
		}
		
		// add buttons tab
		obj.addButton = function(oNode){
			obj.buttons.push(oNode);
		}
		
		obj.newTab = function(options){
			if(options.name){
				obj.otabs.newTab({
					name: options.name,
					html: options.html,
					action: function(){
						if(!obj.opened){
							obj.show();
						}
						else {
							//obj.hide();
						}
					}
				});
			}
		}
		
		obj.print = function(){
			var oButton = new NW3.form.Button('hide_show');
			oButton.setClass('NWToolboxButtonExpandHide');
			oButton.setTitle(NW3.lang.Hide);
			oButton.onMouseUp(function(){
				/*if(!obj.opened){
					obj.show();
				}
				else {*/
					obj.hide();
				//}
				if(obj.buttoncloseevent) obj.buttoncloseevent();
			});
			
			obj.object.addButton(oButton);
			
			obj.otabs.setAlign('bottom');
			/*for(bb=0; obj.buttons.length>bb; bb++){
				obj.buttons[bb].onMouseUp(function(){
					if(!obj.opened){
						obj.show();
					}
					else {
						//obj.hide();
					}
				});
				obj.otabs.add(obj.buttons[bb]);
			}*/
			
			obj.otabs.setPressed(1);
			
			obj.add(obj.otabs.output());
			
			//obj.object.addNwObjStatusBar(obj.otabs);
			
			// oggetto interno finestra
			/*var oTable = new NW3.table.Table;
			oTable.setClass('NWToolboxTable');
			var oTr = new NW3.table.Tr;
			var oTd1 = new NW3.table.Td;
			
			oTd1.add(oText1);
			oTd1.setClass('NWToolboxTableTdLeftLeft');
			oTr.add(oTd1);
			
			//document.body.appendChild(oTabs.output());
			
			//oTd2.add(oButton.output());
			oTd2.setClass('NWToolboxTableTdRightLeft');
			oTr.add(oTd2);
			
			oTable.add(oTr);
			
			obj.table = oTable;*/
			
			//obj.object.add(obj.table.output());
			
			// aggiungo contenuti
			for(oc=0;obj.contents.length>oc;oc++){
				obj.object.add(obj.contents[oc]);
			}
			
			obj.object.setTitle(obj.title);
			//obj.object.setStatusText('Barra di stato');
			
			if(obj.position == 'right') obj.floatRight();
			else obj.floatLeft();
			
			//obj.table.display(false);
			
			obj.object.print();
			
			// visualizzo finestra
			obj.object.show();
			
			// aggiorna posizione al resize della window se align right
			if(obj.position == 'right'){
				//oWindowResizeEvent = new NW3.event.Handling(window);
				NW3.event.addEvent(window, 'resize', function(){
					obj.floatRight();
					if(obj.opened){
						obj.quickShow();
					}
				});
			}
		}
		
		obj.replace = function(targetElement){
			NW3.core.object.replace(targetElement, obj.output());
		};
		
		obj.insertAfter = function(targetElement){
			NW3.core.object.insertAfter(obj.output(), targetElement);
		};
		
		obj.insertBefore = function(targetElement){
			NW3.core.object.insertBefore(obj.output(), targetElement);
		};
		
		obj.appendChild = function(targetElement){
			NW3.core.object.appendChild(obj.output(), targetElement);
		};
		
		return obj;
	},
	
	Button : function(){
		var oButton = new NW3.tabs.Button();
		
		return oButton;
	}
}